'use strict'

const Controller = require('egg').Controller
const { isEqual } = require('lodash')

class LoginController extends Controller {
    //注册
    async register() {
        const { ctx, app } = this
        // 设置私钥
        const prvKey = this.app.config.private_key
        let jsencrypt = new JSEncrypt()
        jsencrypt.setPrivateKey(prvKey)
        // 解密数据
        let paramsData = ctx.request.body.rsaParams
        let prvData = JSON.parse(jsencrypt.decrypt(paramsData))
        //查找注册账号是否存在
        const is_user_numnber = await ctx.model.User.findOne({ where: { user_number: prvData.user_number } })
        if (!prvData.user_number || prvData.user_number == '') {
            return ctx.warn('账号不能为空')
        } else if (!prvData.user_password || prvData.user_password == '') {
            return ctx.warn('密码不能为空')
        } else if (is_user_numnber != null) {
            return ctx.warn('账号已被注册')
        } else {
            //对密码进行hash加密,salt加盐
            const salt = bcrypt.genSaltSync(10)
            const user_password = bcrypt.hashSync(prvData.user_password, salt)
            let res = {}
            const params = {
                uuid: ctx.helper.uuidSet(), //此方法为扩展方法，在/extend/helper.js 中，用于生成uuid
                user_name: ctx.helper.makeName(), //此方法为扩展方法，在/extend/helper.js 中，用于生成随机昵称
                user_number: prvData.user_number,
                user_password: user_password,
                user_type: 0,
                salt: salt,
                create_time: new Date(),
            }
            try {
                await ctx.model.UserLogin.create(params)
                res = await ctx.model.User.create(params)
                ctx.logger.info(res)
                return ctx.success('注册成功') //此方法为扩展方法，在/extend/context.js 中，用于处理返回数据
            } catch (err) {
                ctx.logger.error(err)
                return ctx.fail(err)
            }
        }
    }

    async login() {
        const { ctx, service } = this
        const { account, passwd } = ctx.request.body
        let res = { status: 0, token: {} }
        if (account && passwd) {
            const user = await service.user.login(account, passwd)
            if (!isEqual(user, {})) {
                res = {
                    status: 0,
                    ...user,
                }
            } else {
                res.status = 1
                res.token = ''
                res.account = ''
            }
        }
        ctx.body = res
    }

    async loginout() {
        /**
         * @summary 退出登录
         * @description 退出登录接口
         * @router post /user/logout
         * @Request header string token eg：write your token at here
         * @response 200 JsonResult 操作结果
         */
        const { ctx } = this
        ctx.body = await this.service.user.logout()
    }

    async getUserByAccount() {
        const { ctx } = this
        const user = await this.service.user.getUserByAccount(ctx.request.body.account)
        ctx.body = {
            status: 0,
            user,
        }
    }
}

module.exports = LoginController
